<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends AuthController {

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index(){
	 $this->display();
    }
	
	 /*主体布局*/
	 public function main(){
	     $domain  = M("domain");
		 $vps     = M("vps");
		 $host    = M("host");
		 $group   = M("group");
		 $where['status']   = 1;
	     $domain_all = $domain -> where($where) -> count();
		 $vps_all    = $vps    -> where($where) -> count();
		 $host_all   = $host   -> where($where) -> count();
		 
		 /*急需续费网站*/
		 $today = time();
		 $map ="(DATEDIFF(from_unixtime(endtime,'%Y-%m-%d'), from_unixtime($today,'%Y-%m-%d'))) < 45";
		 $map.=" AND (DATEDIFF(from_unixtime($today,'%Y-%m-%d'), from_unixtime(endtime,'%Y-%m-%d'))) < 30";
		 $domain_jixulist = $domain -> where($map) ->limit(6)  -> select();
		 $domain_jixu     = $domain -> where($map) -> count();
		
	    
		/*遍历查询过去12个月的每个月新增网站，存储为json*/
		$current =time();
		for($i = 0; $i < 12; $i++){
		    $where = "from_unixtime(inputtime,'%Y-%m') = from_unixtime($current,'%Y-%m')";
		    $tongji[$i]['num'] = $domain ->where($where) ->count();
			$tongji[$i]['year'] = date('Y-m',$current);
			$current = strtotime("-1 Months", $current);
		}
		$tongji = json_encode(array_reverse($tongji));//倒置数组
		$group_info = $group -> field('groupname,webnum')->select();
		$group_num = json_encode($group_info);
		$patten = '/\"webnum\":\"(\d+)\"/';//过滤json中的数字含有双引号的问题
		$group_num = preg_replace($patten ,'"webnum":$1', $group_num);
		
		/*本月新增网站*/
		$current =time();
		$where2 = "from_unixtime(inputtime,'%Y-%m') = from_unixtime($current,'%Y-%m')";
		$web_month = $domain ->where($where2) ->order('listorder desc,did desc') ->limit(6) -> select();
		
		
		$this->assign(array(
		     'domain_all' => $domain_all,
			 'vps_all'    => $vps_all,
			 'domain_jixu'=> $domain_jixu,
			 'host_all'   => $host_all,
			 'tongji'     => $tongji,
			 'group_num'  => $group_num,
			 'web_month' => $web_month,
			 'domain_jixulist' => $domain_jixulist,
		 ));
		
		$this->display();
    }
}